package com.admin.business.voteRecord;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description: 发起投票公示记录表
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteRecord")
@Slf4j
public class VoteRecordService{

    @Resource
    VoteRecordDao voteRecordDao;




    //-----------------------------------2022-11-09，代码重构------------------------------------------

    /**
     * 添加发起投票公示记录表,获取id数据
     *
     * @param cdoRequest - id:
     *                   - user_id:发起人id
     *                   - essential_information_id:基本信息id
     *                   - vote_name:投票名称
     *                   - vote_state:是否结束：否/是 : 0/1
     *                   - apply_reconsideration_total:申请复议总户数
     *                   - apply_reconsideration_area:申请复议总面积
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     *                    9月7 刚加个contentL 招募信息内容字段
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return addVoteRecord(CDO cdoRequest) {

//        一次会议只有一条发布通知公告信息
        CDO coutCha=new CDO();
        coutCha.set("essential_information_id",cdoRequest.getString("essential_information_id"));
        coutCha.set("vote_name",cdoRequest.getString("vote_name"));
        int voteRecordCount = voteRecordDao.getVoteRecordListByConditionCount(coutCha);
        CDO cdoResult = new CDO();
        if (voteRecordCount==0){
            voteRecordDao.addVoteRecord(cdoRequest);
        }else {
            cdoResult.set("已添加","null");
        }

        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新发起投票公示记录表数据
     *
     * @param cdoRequest - id:
     *                   - user_id:发起人id
     *                   - essential_information_id:基本信息id
     *                   - vote_name:投票名称
     *                   - vote_state:是否结束：否/是 : 0/1
     *                   - apply_reconsideration_total:申请复议总户数
     *                   - apply_reconsideration_area:申请复议总面积
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteRecordById(CDO cdoRequest) {
        int affectedRows = voteRecordDao.updateVoteRecordById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除发起投票公示记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteRecordById(CDO cdoRequest){
        int affectedRows = voteRecordDao.deleteVoteRecordById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除发起投票公示记录表数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteRecord(CDO cdoRequest) {
        int affectedRows = voteRecordDao.deleteVoteRecord(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取发起投票公示记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecordById(CDO cdoRequest) {
        CDO cdoMapperResult = voteRecordDao.getVoteRecordById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取发起投票公示记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecord(CDO cdoRequest)  {
        CDO cdoMapperResult = voteRecordDao.getVoteRecord(cdoRequest);

        return Return.valueOf(cdoMapperResult);
    }

    @OpenApi
    public Return getVoteRecordPreparation(CDO cdoRequest)  {
        CDO cdoMapperResult = voteRecordDao.getVoteRecord(cdoRequest);

        if (cdoMapperResult==null){
            return Return.valueOf(-2,"公开招募筹备组成员未发布");
        }
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date begin_time1 = cdoMapperResult.getDate("begin_time");
        if(begin_time1!=null){
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(begin_time1);
            cal2.add(Calendar.DATE, +7);
            Date begin_time = cal2.getTime();
            cdoMapperResult.set("afterTime",sdf.format(begin_time));
        }
        return Return.valueOf(cdoMapperResult);
    }




    /**
     * 根据非空条件，获取发起投票公示记录表数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecordList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteRecordDao.getVoteRecordList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteRecordDao.getVoteRecordListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取发起投票公示记录表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecordListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteRecordDao.getVoteRecordListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteRecordDao.getVoteRecordListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
